<?php

namespace app\admin\controller;

use think\auth\Auth;
use think\auth\AuthManager;
use think\Controller;
use think\captcha\Captcha;
use think\Config;
use app\admin\model\User;
use app\admin\model\Menu;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthRule;
use think\Request;

/**
 * Class Index
 * @package app\admin\controller
 * 登录和进入主页不进行权限校验但是进行登录校验
 */
class Index extends Controller
{
    private $config;

    protected function _initialize()
    {
        parent::_initialize();
        $this->config = Config::get('login_admin');
    }

    public function index()
    {
        //获取左侧菜单数据
        $menu = new Menu();
        $data = $menu->getChild();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function test()
    {
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $mixed = session(null);
        $this->redirect('login');
    }

    /**
     * @return mixed
     * 登录和登录校验
     */
    public function login()
    {
        $data = input('post.');
        $config = Config::get('login_admin');
        if (empty($data)) {
            return $this->fetch();
        } else {
            $validate = new \app\admin\validate\Admins;
            if (!$validate->check($data)) {
                $this->error($validate->getError(), 'login');
            } else {
                $user = User::where('username', $data['username'])->find();
                $userValidate=new \app\admin\validate\User;
                if (!$userValidate->check($user)) {
                    $this->error($userValidate->getError(), 'login');
                } else {
                    if ($user) {
                        if ($user->password == md5($data['password'] . $user->token)) {
                            if (!captcha_check($data['captcha'])) {
                                $this->error('验证码错误,请重新登录!');
                            } else {
                                //更新ip
                                $userUpdate=User::get($user->id);
                                $userUpdate->last_login_ip=Request::instance()->ip();
                                $userUpdate->last_login_time=date("Y-m-d H:i:s");
                                $userUpdate->save();
                                session('username', $userUpdate->username);
                                session($config['auth_uid'], $userUpdate->id);
                                session('last_login_ip', $userUpdate->last_login_ip);
                                session('last_login_time', $userUpdate->last_login_time);
                                if ($user->id == 1) {
                                    session('super', "超级管理员");
                                }
                                $this->redirect('index');
                            }
                        } else {
                            $this->error('账号或者密码错误');
                        }
                    } else {
                        $this->error('账号或者密码错误');
                    }
                }
            }
        }

    }

    public function welcome()
    {
        return $this->fetch();
    }

    //验证码
    public function captcha()
    {
        $config = Config::get('captcha');
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    //404页面
    public function e404()
    {
        return $this->fetch('404');
    }

    //403页面
    public function e302()
    {
        return $this->fetch('302');
    }

}
